package gem.model.course;

import gem.database.Common;

import java.util.ArrayList;
import java.util.Vector;

/*
 * Klasa koja cuva asocijacije iz baze. Kada se izmeni pripadnost
 * cuvamo prethodnu, kako bismo mogli da azuriramo.
 */
public class Association
{
	//====================================================================================//
	public final int departmentId;
	public final int scienceDomainId;
	public final int courseId;
	
	public int newDepartmentId;
	public int newscienceDomainId;
	public int newcourseId;
	
		
	//====================================================================================//
	public Association( int dep, int sc, int cour )
	{
		this.departmentId = dep;
		this.courseId = cour;
		this.scienceDomainId = sc;
		
		this.newDepartmentId = dep;
		this.newcourseId = cour;
		this.newscienceDomainId = sc;
	}
	//====================================================================================//
	@Override
	public boolean equals(Object obj)
	{
		if( Association.class.isInstance(obj))
		{
			if( this == obj)
			{
				return true;
			}
			Association a = (Association)obj;
			if( ( this.departmentId == a.departmentId ) &&
				( this.scienceDomainId == a.scienceDomainId ) &&
				( this.courseId == a.courseId ) )
			{
				return true;
			}
		}
		return false;
	}
	//====================================================================================//
	@Override
	public int hashCode()
	{
		return ( 41 * ( 41 + departmentId + scienceDomainId + courseId ) );
	}
	//====================================================================================//
	//public static java.util.ArrayList< Association >associations = new ArrayList<Association>();
	public static java.util.Set< Association >associations = new java.util.HashSet< Association>();	
	//====================================================================================//
	public boolean isChanged()
	{
		return (( departmentId != newDepartmentId) ||
				( scienceDomainId != newscienceDomainId ) ||
				( courseId != newcourseId ));
	}
	//====================================================================================//	
	public static void changeAssociations( int oldDep, int oldSD, int oldC, int newDep, int newSD, int newC )
	{
		for( Association a : associations )
		{
			if( isEqual(a, oldDep, oldSD, oldC) )
			{
				a.newDepartmentId = newDep;
				a.newscienceDomainId = newSD;
				a.newcourseId = newC;
			}
		}
	}
	//====================================================================================//
	private static boolean isEqual(Association a,  int oldDep, int oldSD, int oldC )
	{
		return( (a.departmentId == oldDep ) && (a.scienceDomainId == oldSD) && (a.courseId == oldC) );
	}
	//====================================================================================//
	public static void addAssociation( Association a )
	{
		associations.add(a);
	}
	//====================================================================================//
	public static void addAssociation( int dep, int sd, int c )
	{
		associations.add( new Association(dep, sd, c) );
	}
	//====================================================================================//
	public static void departmentChanged( ScienceDomain sd, Department oldDep, Department newDep )
	{
		int oldDepId = oldDep.getId();
		int newDepId = newDep.getId();
		int sdId = sd.getId();
		
		if( newDepId == Common.DB_NEW_ENTRY )
		{
			// Not allowed yet to add to new department
			return;
		}
		
		for( Association a : associations )
		{
			if( a.scienceDomainId == sdId )
			{
				a.newDepartmentId = newDepId;
			}
		}
	}
	//====================================================================================//
	public static void scienceDomainChanged( Course c, ScienceDomain oldSD, ScienceDomain newSD )
	{
		int courseId = c.getId();
		int oldSdId = oldSD.getId();
		int newSDId = newSD.getId();
		
		if( newSDId == Common.DB_NEW_ENTRY )
		{
			// Not allowed yet to add Course to new ScienceDomain
			return;
		}
		
		for( Association a : associations )
		{
			if( a.courseId == courseId )
			{
				a.newscienceDomainId = newSDId;
				a.newDepartmentId = newSD.getDepartment().getId();
			}
		}
	}
	//====================================================================================//
	//====================================================================================//
	//====================================================================================//
	//====================================================================================//
}
